package com.integration.rest.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.core.StreamingOutput;

import org.json.JSONArray;
import org.json.JSONObject;

import com.integration.pojo.FileDTO;

public class RetrieveService implements IRetrieveService {

	public String getJSONList(String sharing) {
		System.out.println("sharing: " + sharing);
		List<FileDTO> files = new ArrayList<FileDTO>();

		FileDTO fileInfo = new FileDTO();
		fileInfo.setId("3662");
		fileInfo.setName("artigo.pdf");
		fileInfo.setTimestamp((new Date()).toString());
		List<String> owners = new ArrayList<String>();
		owners.add("frida");
		owners.add("obelix");
		fileInfo.setOwners(owners);
		fileInfo.setSize(BigInteger.valueOf(123899898l));
		files.add(fileInfo);

		fileInfo = new FileDTO();
		fileInfo.setId("3664");
		fileInfo.setName("musica.ogg");
		fileInfo.setTimestamp((new Date()).toString());
		owners = new ArrayList<String>();
		owners.add("fritz");
		owners.add("luigi");
		fileInfo.setOwners(owners);
		fileInfo.setSize(BigInteger.valueOf(5594564654l));
		files.add(fileInfo);
		files.add(fileInfo);
		
		JSONArray jsonArray = new JSONArray(files);

		return jsonArray.toString();
	}

	@Override
	public String getJSONItem(String sharing, String fileId) {
		FileDTO bean = new FileDTO();
		bean.setId("9988");
		bean.setName("arquivo.exe");
		bean.setTimestamp((new Date()).toString());
		List<String> owners = new ArrayList<String>();
		owners.add("fritz");
		bean.setOwners(owners);
		bean.setSize(BigInteger.valueOf(5464564546546l));
		return new JSONObject(bean).toString();
	}
	@Override
	public StreamingOutput getItem(String sharing, String fileId) throws FileNotFoundException {
		String fileName = fileId;
		StreamingOutput output = new FileStreamingOutput(new FileInputStream(new File(fileName)));
		return output;
	}

}
